<?php
/*
 * [JINYUN!] (C)2001-2099 Jinyunweb.com
 * This is NOT a freeware, use is subject to license terms
 * $Id: 2017-12-13 03:39:35 apple $
*/
defined('BY_JYA') or exit('error');
$_RQ['op']=$_RQ['op']?$_RQ['op']:'main';
cfc('core')->checklogin('jy_jdpm/member.index/');
$return=array();
$return['channel'] = pdo_getall('jy_jdpm_goods_group',array('uniacid'=>$_SESSION['uniacid'],'pid' => 0),'','','paixu asc');
set_html('title','地址-'.$this->plugin_setting('basic')['title']);
if($_RQ['op']=='main'){
	$membersinfo=pdo_get('jy_jdpm_member',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid']));
	$address = pdo_getall('core_address',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid']));
	if($address){
		foreach($address as &$item){
			$item['provaddress']=array(
				'province'=>$item['prov'],
				'city'=>$item['city'],
				'district'=>$item['district'],
			);
		}
	}
	$return['list']=$address;
	$return['membersinfo']=$membersinfo;
	exi($return);
}
if($_RQ['op']=='edit'){
	$adid=$_RQ['id'];
	if($adid){
		$address = pdo_get('core_address',array('uniacid'=>$_SESSION['uniacid'],'adid'=>$adid));
		
		$address['provaddress']=array(
			'province'=>$address['prov'],
			'city'=>$address['city'],
			'district'=>$address['district'],
		);
		
	}else{
		$address = array();
	}
	$return['info']=$address;
	$return['province']=file_get_contents('core/resource/area.txt');
	$return['province']=unserialize($return['province']);
	exi($return);
}
if($_RQ['op']=='del'){
	$adid=$_RQ['id'];
	$post=get_postdata();
	$addess=pdo_get('core_address',array('uniacid'=>$_SESSION['uniacid'],'adid'=>$post['id']));
	if($post['id'] &&$addess){
		pdo_delete('core_address',array('uniacid'=>$_SESSION['uniacid'],'adid'=>$post['id']));
	}else{		
		exi('地址不存在','error');
	}
	exi('删除成功','','main');
}
if($_RQ['op']=='post'){
	$post=get_postdata();
	if(!$post['provaddress']){
		exi('请选择地区');
	}
	if(!$post['mobile']){
		exi('请填写手机号');
	}
	if(!$post['truename']){
		exi('请填写姓名');
	}
	if(!$post['address']){
		exi('请填写详细地址');
	}
	$post['prov']=$post['provaddress']['province'];
	$post['city']=$post['provaddress']['city'];
	$post['district']=$post['provaddress']['district'];
	unset($post['provaddress']);
	$post['provaddress']=$post['prov'].$post['city'].$post['district'];
	if($post['default']==1){
		pdo_update('core_address',array('default'=>0),array('mid'=>$_SESSION['uid']));
	}
	if($post['adid']){		
		pdo_update('core_address',$post,array('uniacid'=>$_SESSION['uniacid'],'adid'=>$post['adid']));
		exi('编辑成功','','main');
	}else{
		$post['mid']=$_SESSION['uid'];
		pdo_insert('core_address',$post,array('uniacid'=>$_SESSION['uniacid'],'adid'=>$post['adid']));
		exi('添加成功','','main');
	}
	
}
if($_RQ['op']=='set'){
	$adid=intval($_RQ['id']);
	if(!$id){
		exi('请先选择要设置的地址','error');
	}
	pdo_update('core_address',array('default'=>0),array('uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid'],'default'=>1));
	pdo_update('core_address',array('default'=>1),array('uniacid'=>$_SESSION['uniacid'],'id'=>$adid,'mid'=>$_SESSION['uid']));
	exi('设置成功！');
}